using System;
using System.IO;
using System.Text;
using System.Diagnostics;

using A4G.Core;

namespace A4G.Assignment1
{
	public class Program
	{
		static void PrintHelp()
		{
			Console.WriteLine("Algorithm For Genomes Assignment 1.");
			Console.WriteLine("Start as follows:");
			Console.WriteLine("a4g.assignment1 <filename> <W>");
			Console.WriteLine("Where <filename> is the file containing the fasta sequences and");
			Console.WriteLine("<W> is the length of the motif to find.");
		}

		static void Main(string[] args)
		{
			Debug.Assert(args.Length >= 2, "Insufficient program arguments");

			string filename = args[0];
			if (!File.Exists(filename))
			{
				Console.WriteLine("Filename incorrect.");
				Console.WriteLine();
				PrintHelp();
				return;
			}

			int W = 0;
			try
			{
				W = Convert.ToInt32(args[1]);
			}
			catch
			{
				Console.WriteLine("W incorrect.");
				Console.WriteLine();
				PrintHelp();
				return;
			}

			int n = 1000;
			try
			{
				if (args.Length > 2)
				{
					n = Convert.ToInt32(args[2]);
				}
			}
			catch
			{
			}

			FastaFile inputFile = new FastaFile(filename);
			inputFile.Load();

			// To check if file was loaded properly (to be able to perform a diff)
			//FastaFile a4gFile = new FastaFile(Path.ChangeExtension(filename, "a4g"));
			//a4gFile.Save();

			GibbsSampler sampler = new GibbsSampler((Sequence[])inputFile.Sequences.ToArray(typeof(Sequence)), W, filename);
			sampler.Start(n);
			sampler.WriteMotifs(Path.ChangeExtension(filename, "a4g.output"), W, n);
		}
	}
}
